package br.com.inner.dao;

import java.math.BigDecimal;
import java.util.Date;
import java.util.List;

import javax.persistence.EntityManager;
import javax.persistence.Query;

import org.apache.commons.lang.StringUtils;

import br.com.inner.custom.CustomException;
import br.com.inner.custom.JPAManager;
import br.com.inner.modelo.Job;
import br.com.inner.modelo.JobEmpresa;
import br.com.inner.modelo.StatusJob;
import br.com.inner.modelo.Usuario;

public class JobDBDao implements JobDao {

	public void delete(BigDecimal cdJob) {
		// TODO Auto-generated method stub
		try {
			Job job = null;
			EntityManager em = JPAManager.getEntityManager();
			//em.clear();
			JPAManager.beginTransaction();
			job = em.find(Job.class, cdJob);
			em.remove(job);
			JPAManager.commitTransaction();
		} catch (Exception e) {
			// TODO: handle exception
			JPAManager.rollbackTransaction();
			e.printStackTrace();
		}
	}

	public List<Job> getJob(Usuario usu) throws CustomException {
		EntityManager em = JPAManager.getEntityManager();
		Query q = em.createNamedQuery("listarJob");
		q.setParameter("cdDestinatario", usu.getCdUsuario());
		return q.getResultList();
	}

	public void insert(Job job) throws CustomException {
		// TODO Auto-generated method stub
		System.out.println("Entrou no insert do JobDbDao");
		EntityManager em = JPAManager.getEntityManager();
		//JPAManager.beginTransaction();
		em.persist(job);
		//JPAManager.commitTransaction();
	}
	
	public Job getDetalheJob(BigDecimal cdJob) throws CustomException {
		EntityManager em = JPAManager.getEntityManager();
		Job job = em.find(Job.class, cdJob);
		return job;
	}
	
	public List<JobEmpresa> getJobDetalheJobEnviado(BigDecimal cdJob) throws CustomException {
		EntityManager em = JPAManager.getEntityManager();
		Query q = em.createQuery("SELECT je FROM JobEmpresa je WHERE je.job.cdJob = :cdJob");
		q.setParameter("cdJob", cdJob);
		List<JobEmpresa> lista = q.getResultList();
		return lista;
	}
	
	public void update(Job job) throws CustomException {
		// TODO Auto-generated method stub
		EntityManager em = JPAManager.getEntityManager();
		//JPAManager.beginTransaction();
		em.merge(job);
		//JPAManager.commitTransaction();
	}
	
	public void atualizaJob(BigDecimal cdJob, BigDecimal cdUsuario, StatusJob status)throws CustomException {
		try {
			EntityManager em = JPAManager.getEntityManager();
			em.clear();
			JPAManager.beginTransaction();
			Usuario usu = em.find(Usuario.class, cdUsuario);
			
			Query q = em.createQuery("SELECT je FROM JobEmpresa je WHERE je.job.cdJob = :cdJob AND je.empresa.cdEmpresa = :cdEmpresa");
			q.setParameter("cdJob", cdJob);
			q.setParameter("cdEmpresa", usu.getEmpresa().getCdEmpresa());
			
			JobEmpresa job = (JobEmpresa)q.getSingleResult();
			job.setStatus(status);
			
			em.merge(job);
			JPAManager.commitTransaction();
		} catch (Exception e) {
			// TODO: handle exception
			JPAManager.rollbackTransaction();
			e.printStackTrace();
		}
		
	}
	
	public JobEmpresa getJobEmpresa(BigDecimal cdJob, BigDecimal cdUsuario)throws CustomException {
		JobEmpresa job = null;
		try {
			EntityManager em = JPAManager.getEntityManager();
			em.clear();
			JPAManager.beginTransaction();
			Usuario usu = em.find(Usuario.class, cdUsuario);
			
			Query q = em.createQuery("SELECT je FROM JobEmpresa je WHERE je.job.cdJob = :cdJob AND je.empresa.cdEmpresa = :cdEmpresa");
			q.setParameter("cdJob", cdJob);
			q.setParameter("cdEmpresa", usu.getEmpresa().getCdEmpresa());
			
			job = (JobEmpresa)q.getSingleResult();

			JPAManager.commitTransaction();
			
			return job;
		} catch (Exception e) {
			// TODO: handle exception
			JPAManager.rollbackTransaction();
			e.printStackTrace();
			return job;
		}
		
	}

}
